package org.jeecg.modules.avsp.one.service.impl;

import org.jeecg.modules.avsp.one.entity.AvspAlbum;
import org.jeecg.modules.avsp.one.entity.AvspFile;
import org.jeecg.modules.avsp.one.mapper.AvspFileMapper;
import org.jeecg.modules.avsp.one.mapper.AvspAlbumMapper;
import org.jeecg.modules.avsp.one.service.IAvspAlbumService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 影音集
 * @Author: jeecg-boot
 * @Date:   2021-02-08
 * @Version: V1.0
 */
@Service
public class AvspAlbumServiceImpl extends ServiceImpl<AvspAlbumMapper, AvspAlbum> implements IAvspAlbumService {

	@Autowired
	private AvspAlbumMapper avspAlbumMapper;
	@Autowired
	private AvspFileMapper avspFileMapper;
	
	@Override
	@Transactional
	public void saveMain(AvspAlbum avspAlbum, List<AvspFile> avspFileList) {
		if(avspFileList!=null && avspFileList.size()>0) {
			////////根据“,”分割文件名称，再循环存到附表中////////
			String filesysname=avspFileList.get(0).getFileSysname();
			String arr[]=filesysname.split(",");
			avspAlbum.setFileCount(arr.length);
			avspAlbum.setAlbumState("S");////设置状态为审核中
			avspAlbumMapper.insert(avspAlbum);
			for(int i=0;i< arr.length;i++) {
				AvspFile entity=new AvspFile();
				entity.setCreateBy(avspFileList.get(0).getCreateBy());
				entity.setCreateTime(avspFileList.get(0).getCreateTime());
				entity.setSysOrgCode(avspFileList.get(0).getSysOrgCode());
				//外键设置
				entity.setAlbumId(avspAlbum.getId());
				entity.setFileSysname(arr[i]);
				String file=arr[i];
				String format[]=file.split("\\u002E");////分割“.”获取文件格式
				entity.setFileFormat(format[format.length-1]);
				avspFileMapper.insert(entity);
			}
		}else
		{
			avspAlbum.setFileCount(0);
			avspAlbum.setAlbumState("S");////设置状态为审核中
			avspAlbumMapper.insert(avspAlbum);
		}
	}

	@Override
	@Transactional
	public void updateMain(AvspAlbum avspAlbum,List<AvspFile> avspFileList) {
		//avspAlbumMapper.updateById(avspAlbum);

		if(avspFileList!=null && avspFileList.size()>0) {
			String filesysname=avspFileList.get(0).getFileSysname();
			String arr[]=filesysname.split(",");
			avspAlbum.setFileCount(arr.length);
			avspAlbum.setAlbumState("S");////设置状态为审核中
			avspAlbumMapper.updateById(avspAlbum);
			//1.先删除子表数据
			avspFileMapper.deleteByMainId(avspAlbum.getId());
			//2.子表数据重新插入
			for(int i=0;i< arr.length;i++) {
				AvspFile entity=new AvspFile();
				entity.setCreateBy(avspFileList.get(0).getCreateBy());
				entity.setCreateTime(avspFileList.get(0).getCreateTime());
				entity.setSysOrgCode(avspFileList.get(0).getSysOrgCode());
				entity.setUpdateBy(avspFileList.get(0).getCreateBy());
				entity.setUpdateTime(avspFileList.get(0).getCreateTime());
				//外键设置
				entity.setAlbumId(avspAlbum.getId());
				entity.setFileSysname(arr[i]);
				String file=arr[i];
				String format[]=file.split("\\u002E");////分割“.”获取文件格式
				entity.setFileFormat(format[format.length-1]);
				avspFileMapper.insert(entity);
			}
		}else {
			avspAlbum.setFileCount(0);
			avspAlbum.setAlbumState("S");////设置状态为审核中
			avspAlbumMapper.updateById(avspAlbum);
			//1.先删除子表数据
			avspFileMapper.deleteByMainId(avspAlbum.getId());
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		avspFileMapper.deleteByMainId(id);
		avspAlbumMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			avspFileMapper.deleteByMainId(id.toString());
			avspAlbumMapper.deleteById(id);
		}
	}
	
}
